(function(){
    var Loader = {
        base: "http://beta.api.mapy.cz",
        mode: "single",
        version: "34",
        async: false,
        _callback: false,
        _files: {
            single: {
                jak: "/js/api/jak.js",
                api: ["/js/api/smap.js", "/config.js?key={key}"],
                "api-simple": [
                    "/js/api/smap-simple.js",
                    "/config.js?key={key}"
                ]
            },
            multi: {
                jak: "/js/api/jak/jak.js",
                api: [
                    "/js/api/jak/graphics.js" ,
                    "/js/api/jak/window.js" ,
                    "/js/api/jak/xml.js" ,
                    "/js/api/jak/timekeeper.js" ,
                    "/js/api/jak/interpolator.js" ,
                    "/js/api/api/map.js" ,
                    "/js/api/api/map-iowned.js" ,
                    "/js/api/api/util.js" ,
                    "/js/api/api/control.js" ,
                    "/js/api/api/layer.js" ,
                    "/js/api/api/layer-tile.js" ,
                    "/js/api/api/layer-poi.js" ,
                    "/js/api/api/layer-geometry.js" ,
                    "/js/api/api/geometry.js" ,
                    "/js/api/api/geometry-poi.js" ,
                    "/js/api/api/marker.js" ,
                    "/js/api/api/marker-poi.js" ,
                    "/js/api/api/card.js" ,
                    "/js/api/api/control-layer.js" ,
                    "/js/api/api/control-zoom.js" ,
                    "/js/api/api/control-copyright.js" ,
                    "/js/api/api/poiserver.js" ,
                    "/js/api/api/contextmenu.js" ,
                    "/js/api/api/contextmenu-item.js" ,
                    "/js/api/util/gpx.js" ,
                    "/js/api/util/kml.js" ,
                    "/js/api/util/geocoder.js" ,
                    "/js/api/util/logger.js" ,
                    "/js/api/api/eggs.js" ,
                    "/config.js?key={key}" ],
                "api-simple": [
                    "/js/api/api/map-simple.js" ,
                    "/js/api/api/util.js" ,
                    "/config.js?key={key}"
                ]
            }
        },
        load: function(key_, what_, callback) {
            var key = key_ || "";
            var what = { jak: true, api: "full" };
            for (var p in what_) {
                what[p] = what_[p];
            }
            if (callback) {
                this._callback = callback;
            } /* soupis souboru k naloadovani */
            var list = [];
            var files = this._files[this.mode];
            if (what.jak) {
                list = list.concat(files.jak);
            }
            if (what.api == "full") {
                list = list.concat(files.api);
            }
            if (what.api == "simple") {
                list = list.concat(files["api-simple"]);
            } /* vyrobit celou cestu */
            for (var i=0;i<list.length;i++) {
                var value = list[i];
                value = value.replace(/{key}/, key);
                if (value.indexOf("?") != -1) {
                    value += "&";
                } else {
                    value += "?";
                }
                value += "v=" + (this.version == 0 ? Math.random() : this.version);
                list[i] = this.base + value;
            }
            this._loadList(list);
        },
        _onLoad: function() {
            this.async = true;
        },
        _loadAsync: function(list) {
            var head = document.getElementsByTagName("head")[0];
            function readyStateChange(e) {
                var elm = e.srcElement;
                if (elm.readyState == 'loaded' || elm.readyState == 'complete') {
                    loadNext();
                }
            }
            function loadNext() {
                if (!list.length) {
                    if (Loader._callback) {
                        Loader._callback();
                    }
                    return;
                }
                var name = list.shift();
                var script = document.createElement("script");
                if (script.attachEvent) {
                    script.attachEvent("onreadystatechange", readyStateChange);
                } else {
                    script.addEventListener("load", loadNext, false);
                }
                script.type = "text/javascript";
                script.src = name; head.appendChild(script);
            } loadNext();
        },
        _loadSync: function(list) {
            for (var i=0;i<list.length;i++) {
                document.write('<script type="text/javascript" src="'+list[i]+'"></script>');
            }
            if (this._callback) {
                this._callback();
            }
        },
        _loadList: function(list) {
            if (this.async) {
                this._loadAsync(list);
            } else {
                this._loadSync(list);
            }
        }
    };
    window.Loader = Loader;
    window.onload = function() {
        Loader._onLoad();
    };
})(); 

